package util.io;

import java.io.*;
import java.net.*;

public class URLInput {

	/**
	 * @param args
	 * @throws IOException
	 * @throws MalformedURLException
	 */

	
	
	public static String readFromURL(String url_) {
		StringBuffer buffer = new StringBuffer();

		try {
			URL url = new URL(url_);

			URLConnection connection = url.openConnection();
			connection.setDoInput(true);
			InputStream inStream = connection.getInputStream();
			BufferedReader input = new BufferedReader(new InputStreamReader(
					inStream));

			String line = "";
			while ((line = input.readLine()) != null) {
				buffer.append(line + "\n");

			}
		} catch (Exception e) {
			System.out.println(e.toString());
		}

		return buffer.toString();
	}

	public static void writeURLToFile(String url, String path)
			throws MalformedURLException, IOException {
		java.io.BufferedInputStream in = new java.io.BufferedInputStream(
				new java.net.URL(url).openStream());
		java.io.FileOutputStream fos = new java.io.FileOutputStream(path);
		java.io.BufferedOutputStream bout = new BufferedOutputStream(fos, 1024);
		byte data[] = new byte[1024];

		int count;

		while ((count = in.read(data, 0, 1024)) != -1) {
			bout.write(data, 0, count);
		}

		bout.close();

		in.close();
	}

	public static void main(String args[]) throws MalformedURLException,
			IOException {

		String url = "http://feeds.delicious.com/v2/json/urlinfo/1b65afee553a40b0ffb6d2fbe85dc32e";
		readFromURL(url);

	}
}
